package com.open.launcher.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.open.launcher.R;
import com.open.launcher.base.MyApplication;
import com.open.launcher.base.SysApplication;
import com.open.launcher.presenter.ExitPresenter;
import com.open.launcher.utils.Consts;
import com.open.launcher.utils.Paths;
import com.open.library.utils.PreferencesUtils;


public class ExitActivity extends Activity implements ExitPresenter.Callback {
    private Button nextButton;
    private Button middelButton;
    private Button exit_button;
    private Button continue_button;
    private Button collect_button;
    private Button rightButton;
    private RecyclerView recommend_rv;
    private ExitPresenter presenter;
    private ImageView focuseView;
    private int LOAD_COMPLETE = 1;
    private int VOD_TYPE = 1001; //电视剧/电影
    private int TELE_TYPE = 1000; //电视剧
    private int LIVE_TYPE = 1002;//直播
    private int TVOD_TYPE = 1003;//直播
    private int NEXT_TYPE = 1004;//下一集
    private int CANCLE_TYPE = 1005;//下一集
    private static final int COLLECTION = 1006;//收藏
    private int playType;
    private RelativeLayout live_layout;
    private RelativeLayout bottom_layout;
    private RelativeLayout exit_top_layout;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == LOAD_COMPLETE){
                ExitPresenter presenter = (ExitPresenter) msg.obj;
                int count = msg.arg1;
//                onBindData(presenter,count);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_exit);
        SysApplication.getInstance().addActivity(this);
        Intent intent = getIntent();
        playType = intent.getIntExtra("playType",-1);
        initView();
        initData();
    }

    private void initData() {
        String hotUrl = "http://"+ PreferencesUtils.getString(this, Consts.IP_ADDR_KEY) + Paths.HOT_PAHT + "/"+6;
        nextButton.requestFocus();
        presenter = new ExitPresenter();
        presenter.setDisplayed(true);
        presenter.setCallback(this);
        presenter.loadVodItem(hotUrl,0);
        if(playType == VOD_TYPE){
            nextButton.setVisibility(View.GONE);
            live_layout.setVisibility(View.GONE);
            exit_top_layout.setVisibility(View.VISIBLE);
            bottom_layout.setVisibility(View.VISIBLE);
        }else if(playType == LIVE_TYPE){
            nextButton.setVisibility(View.GONE);
            bottom_layout.setVisibility(View.GONE);
            exit_top_layout.setVisibility(View.GONE);
        }else if(playType == TELE_TYPE){
            live_layout.setVisibility(View.GONE);
            exit_top_layout.setVisibility(View.VISIBLE);
            bottom_layout.setVisibility(View.VISIBLE);
        }else if(playType == TVOD_TYPE){
            nextButton.setVisibility(View.GONE);
            bottom_layout.setVisibility(View.GONE);
            exit_top_layout.setVisibility(View.GONE);
            collect_button.setVisibility(View.GONE);
        }
        middelButton.setOnClickListener(new OnClickListen());
        rightButton.setOnClickListener(new OnClickListen());
        nextButton.setOnClickListener(new OnClickListen());

        exit_button.setOnClickListener(new OnClickListen());
        continue_button.setOnClickListener(new OnClickListen());
        collect_button.setOnClickListener(new OnClickListen());
    }

    private void initView() {
        nextButton = (Button) this.findViewById(R.id.next_tel);
        middelButton = (Button) this.findViewById(R.id.middle_sure);
        rightButton = (Button) this.findViewById(R.id.right_cancel);
        recommend_rv = (RecyclerView) this.findViewById(R.id.recommend_rv);
        focuseView = (ImageView) this.findViewById(R.id.focus_view);
        bottom_layout = (RelativeLayout) this.findViewById(R.id.bottom_layout);
        live_layout = (RelativeLayout) this.findViewById(R.id.live_layout);
        exit_button = (Button) this.findViewById(R.id.exit_button);
        continue_button = (Button) this.findViewById(R.id.continue_button);
        collect_button = (Button) this.findViewById(R.id.collect_button);
        exit_top_layout = (RelativeLayout) this.findViewById(R.id.exit_top_layout);
        nextButton.setTypeface(MyApplication.face);
        middelButton.setTypeface(MyApplication.face);
        rightButton.setTypeface(MyApplication.face);
//        exit_button.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View view, boolean b) {
//                if (b){
//                    view.setBackgroundResource(R.drawable.mybutton_select);
//                }else {
//                    view.setBackgroundResource(0);
//                }
//            }
//        });
//        continue_button.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View view, boolean b) {
//                if (b){
//                    view.setBackgroundResource(R.drawable.mybutton_select);
//                }else {
//                    view.setBackgroundResource(0);
//                }
//            }
//        });
    }
    private void onBindData(ExitPresenter presenter,int count){
//        RecommendAdaper adapter = new RecommendAdaper(presenter,this);
//        adapter.setCount(count);
//        adapter.setFocuse(focuseView);
//        LinearLayoutManager manager = new LinearLayoutManager(ExitActivity.this);
//        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
//        recommend_rv.setLayoutManager(manager);
//        recommend_rv.setAdapter(adapter);
    }
    @Override
    public void onError(int code, String msg) {

    }

    @Override
    public void onRecommendCompleted(ExitPresenter presenter, int count) {
        Message msg = Message.obtain();
        msg.arg1 = count;
        msg.what = LOAD_COMPLETE;
        msg.obj = presenter;
        handler.sendMessage(msg);
    }
    public class OnClickListen implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.middle_sure){
                setResult(CANCLE_TYPE);
                //Toast.makeText(ExitActivity.this, "CANCLE_TYPE==>:"+CANCLE_TYPE, Toast.LENGTH_SHORT).show();
                finish();
            }else if (v.getId() == R.id.next_tel){
                setResult(NEXT_TYPE);
                finish();
            }else if(v.getId() == R.id.right_cancel){
                finish();
            }else if(v.getId() == R.id.exit_button){
//                setResult(CANCLE_TYPE);
//                finish();
                SysApplication.getInstance().exit();
//                System.exit(0);
            }else if(v.getId() == R.id.continue_button){
                finish();
            }else if(v.getId() == R.id.collect_button){
                Intent intent = new Intent();
                intent.setAction("PlayerActivity");
                intent.putExtra("type", COLLECTION);
                sendBroadcast(intent);
            }
        }
    }
}
